DEF_PROJECT=.
include $(DEF_PROJECT)/scripts/main.mk

.PHONY:all download install build rebuild

all:depend download build install disk

download:depend
	$(MAKE) $@ -f $(DIR_BUILD)/depend/Makefile PID=$(PID)

install:depend download build
	$(MAKE) $@ -f $(DIR_BUILD)/depend/Makefile PID=$(PID)

build:depend download
	#mkdir -p $(DIR_SYSROOT)/{bin,boot,etc/{opt,sysconfig},home,lib,mnt,opt,run}
	#mkdir -p $(DIR_SYSROOT)/{media/{floppy,cdrom},sbin,srv,var}
	#install -d -m 0750 $(DIR_SYSROOT)/root
	#install -d -m 1777 $(DIR_SYSROOT)/{,var}/tmp
	#mkdir -p $(DIR_SYSROOT)/usr/{,local/}{bin,include,lib,sbin,src}
	#mkdir -p $(DIR_SYSROOT)/usr/{,local/}share/{doc,info,locale,man}
	#mkdir -p $(DIR_SYSROOT)/usr/{,local/}share/{misc,terminfo,zoneinfo}
	#mkdir -p $(DIR_SYSROOT)/usr/{,local/}share/man/man{1..8}
	$(MAKE) $@ -f $(DIR_BUILD)/depend/Makefile PID=$(PID)

clean:depend
	$(MAKE) $@ -f $(DIR_BUILD)/depend/Makefile PID=$(PID)
	rm -rf tools
	rm -rf sysroot
ifneq '$(MAKECMDGOALS)' 'initrd'
initrd:depend download build install
endif
initrd:
	rm -rf $(DIR_BUILD)/$(@F)
	mkdir -p $(DIR_BUILD)/$(@F)/{bin,dev,etc,lib,mnt,proc,sbin,sys,root}
	cp -f init $(DIR_BUILD)/$(@F)
	cp -af $(DIR_SYSROOT)/bin/{bash,cat,chmod,cp,ls,lsmod,mkdir,mv,mount,killall} $(DIR_BUILD)/$(@F)/bin
	ln -sf bash $(DIR_BUILD)/$(@F)/bin/sh
	cp -af $(DIR_SYSROOT)/sbin/{blkid,fdisk,insmod,modprobe,udevadm,udevd,switch_root} $(DIR_BUILD)/$(@F)/sbin
	cp -af $(DIR_SYSROOT)/usr/bin/{mknod,find} $(DIR_BUILD)/$(@F)/bin
	cp -af $(DIR_SYSROOT)/lib/modules $(DIR_BUILD)/$(@F)/lib
	cp -af $(DIR_SYSROOT)/lib/udev	$(DIR_BUILD)/$(@F)/lib
	for i in $(DIR_BUILD)/$(@F)/bin/* $(DIR_BUILD)/$(@F)/sbin/* $(DIR_BUILD)/$(@F)/lib/udev/* ;\
	do \
		readelf -d $$i | grep 'Shared library' | cut -d'[' -f2 | sed 's/]//' | while read ii ;\
		do \
			cp -L $(DIR_SYSROOT)/lib/$$ii $(DIR_BUILD)/$(@F)/lib; \
			cp -L $(DIR_SYSROOT)/usr/lib/$$ii $(DIR_BUILD)/$(@F)/lib; \
			cp -L $(DIR_TOOLS)/$(CC_TARGET)/lib/$$ii $(DIR_BUILD)/$(@F)/lib; \
		done ;\
	done;exit 0
	cp -L $(DIR_SYSROOT)/lib/libpthread.so.0 $(DIR_BUILD)/$(@F)/lib
	touch $(DIR_BUILD)/$(@F)/etc/mtab
	touch $(DIR_BUILD)/$(@F)/etc/fstab
	install -d $(DIR_BUILD)/$(@F)/lib/{fireware,udev/devices/{pts,shm}}
	find $(DIR_BUILD)/$(@F) -type f -exec $(addsuffix -,$(CC_TARGET))strip --strip-unneeded {} \;
	chmod +x $(DIR_BUILD)/linux/linux-2.6.39.4/scripts/gen_initramfs_list.sh
	$(DIR_BUILD)/linux/linux-2.6.39.4/scripts/gen_initramfs_list.sh $(DIR_BUILD)/$(@F) >$(DIR_BUILD)/initrd.lst
	sed -i '/^[^n]/s/[0-9]\+ [0-9]\+$$/0 0/' $(DIR_BUILD)/initrd.lst
	$(DIR_BUILD)/linux/linux-2.6.39.4/usr/gen_init_cpio $(DIR_BUILD)/initrd.lst >$(DIR_BUILD)/$(@F).img
	rm -f $(DIR_BUILD)/initrd.lst
	gzip -9 $(DIR_BUILD)/$(@F).img
	mkdir -p $(DIR_SYSROOT)/boot/
	mv -f $(DIR_BUILD)/$(@F).img.gz $(DIR_SYSROOT)/boot/$(@F)
	touch -m $(DIR_BUILD)/$(@F)

disk:depend download build install initrd

rebuild:clean depend download build install initrd disk

ifeq '$(MAKECMDGOALS)' 'rebuild'
download:clean
build:clean
install:clean
endif

depend:
	mkdir -p $(DIR_BUILD)
	mkdir -p $(DIR_BUILD)/$@
	mkdir -p $(DIR_BUILD)/log
	find $(DIR_BUILD)/$@ -name '*.mk' -delete; exit 0
	find $(DIR_BUILD)/log -name '*.log' -delete; exit 0
	@echo '.PHONY:all download install build rebuild' > $(DIR_BUILD)/$@/Makefile
	@echo 'all:;' >> $(DIR_BUILD)/$@/Makefile
	@echo 'download:;' >> $(DIR_BUILD)/$@/Makefile
	@echo 'build:;' >> $(DIR_BUILD)/$@/Makefile
	@echo 'install:;' >> $(DIR_BUILD)/$@/Makefile
	@echo 'clean:;' >> $(DIR_BUILD)/$@/Makefile
	@echo '%:' >> $(DIR_BUILD)/$@/Makefile
	@echo -e '\tmkdir -p $$(DIR_WORK)' >> $(DIR_BUILD)/$@/Makefile
	@echo -e '\t$$(MAKE) $$(MAKECMDGOALS) -C $$(DIR_WORK) -f $$(DIR_ORIG)/Makefile DEF_PROJECT=$$(DEF_PROJECT) >>$(DIR_BUILD)/log/$$@.log 2>&1' >> $(DIR_BUILD)/$@/Makefile
	@echo '-include $(DIR_BUILD)/$@/*.mk' >> $(DIR_BUILD)/$@/Makefile
	$(foreach i,$(call subdir,$(DEF_TARGETS)),$(if $(wildcard $i/Makefile), $(MAKE) $@  -j1 -C$i DEF_PROJECT=$(DEF_PROJECT)/../.. ;exit 0 $_))
	@echo 'download:$$(deps)' >> $(DIR_BUILD)/$@/Makefile
	@echo 'build:$$(deps)' >> $(DIR_BUILD)/$@/Makefile
	@echo 'install:$$(deps)' >> $(DIR_BUILD)/$@/Makefile
	@echo 'clean:$$(deps)' >> $(DIR_BUILD)/$@/Makefile
